<?php
// 本类由系统自动生成，仅供测试用途
class IndexAction extends AdminAction {
	public function _initialize() {
		parent::_initialize (); // RBAC 验证接口初始化
	}
	public function index() {
		$username = $_SESSION ['username']; // 用户名
		$roleid = $_SESSION ['roleid']; // 角色ID
		if ($username == '' || $roleid == '') {
			redirect ( PHP_FILE . C ( 'USER_AUTH_GATEWAY' ) );
			return;
		}
		if ($username == C ( 'SPECIAL_USER' )) { // 如果是无视权限限制的用户，则获取所有主菜单
			$sql = 'SELECT `id`,`title` FROM `think_node` WHERE ( `status` =1 AND `display`=1 AND `level`<>1 ) ORDER BY sort DESC';
		} else { // 更具角色权限设置，获取可显示的主菜单
			$sql = "SELECT `think_node`.`id` as id,`think_node`.`title` as title FROM `think_node`,`think_access` WHERE `think_node`.id=`think_access`.node_id AND `think_access`.role_id=$roleid  AND  `think_node`.`status` =1 AND `think_node`.`display`=1 AND (`think_node`.`level` =0 OR `think_node`.`level` =2)  ORDER BY `think_node`.sort DESC";
		}
		$Model = new Model (); // 实例化一个model对象 没有对应任何数据表
		$main_menu = $Model->query ( $sql );
		$this->assign ( 'main_menu', $main_menu );
		$this->display ();
	}
	public function left() {
		$pid = $_GET ['id']; // 选择子菜单
		$NodeDB = D ( 'Node' );
		$datas = $this->left_child_menu ( $pid );
		$parent_info = $NodeDB->getNode ( array (
				'id' => $pid 
		), 'title' );
		$sub_menu_html = '<dl>';
		foreach ( $datas as $key => $_value ) {
			$sub_array = $this->left_child_menu ( $_value ['id'] );
			$sub_menu_html .= "<dt><a target='_self' href='#' onclick=\"showHide('{$key}');\">{$_value[title]}</a></dt><dd><ul id='items{$key}'>";
			if (is_array ( $sub_array )) {
				foreach ( $sub_array as $value ) {
					$href = empty ( $value ['data'] ) ? 'javascript:void(0)' : $value ['data'];
					$sub_menu_html .= "<li><a id='a_{$value[id]}' onClick='sub_title({$value[id]})' href='/admin.php/{$href}'>{$value[title]}</a></li>";
				}
			}
			$sub_menu_html .= '</ul></dd>';
		}
		$sub_menu_html .= '</dl>';
		$this->assign ( 'sub_menu_title', $parent_info ['title'] );
		$this->assign ( 'sub_menu_html', $sub_menu_html );
		$this->display ();
	}
	private function left_child_menu($pid, $with_self = 0) {
		$pid = intval ( $pid );
		
		$username = $_SESSION ['username']; // 用户名
		$roleid = $_SESSION ['roleid']; // 角色ID
		if ($username == C ( 'SPECIAL_USER' )) { // 如果是无视权限限制的用户，则获取所有主菜单
			$sql = "SELECT `id`,`data`,`title` FROM `think_node` WHERE ( `status` =1 AND `display`=2 AND `level` <>1 AND `pid`=$pid ) ORDER BY sort DESC";
		} else {
			$sql = "SELECT `think_node`.`id` as `id` , `think_node`.`data` as `data`, `think_node`.`title` as `title` FROM `think_node`,`think_access` WHERE `think_node`.id = `think_access`.node_id AND `think_access`.role_id = $roleid AND `think_node`.`pid` =$pid AND `think_node`.`status` =1 AND `think_node`.`display` =2 AND `think_node`.`level` <>1 ORDER BY `think_node`.sort DESC";
		}
		$Model = new Model (); // 实例化一个model对象 没有对应任何数据表
		$result = $Model->query ( $sql );
		
		if ($with_self) {
			$NodeDB = D ( 'Node' );
			$result2 [] = $NodeDB->getNode ( array (
					'id' => $pid 
			), `id`, `data`, `title` );
			$result = array_merge ( $result2, $result );
		}
		return $result;
	}
	public function modify_system() {
		if ($this->isPost ()) {
			$system = M ( 'System' );
			
			$id = isset ( $_POST ['s_id'] ) ? $_POST ['s_id'] : 0;
			if (! $id) {
				return;
			}
			$data ['webname'] = isset ( $_POST ['web_name'] ) ? trim ( $_POST ['web_name'] ) : '';
			$data ['web_keyword'] = isset ( $_POST ['web_keyword'] ) ? trim ( $_POST ['web_keyword'] ) : '';
			$data ['web_discrition'] = isset ( $_POST ['web_discrition'] ) ? trim ( $_POST ['web_discrition'] ) : '';
			$data ['use_yzm'] = $_POST ['yzm'];
			if ($system->where ( "id=" . $id )->save ( $data )) {
				echo "<script>alert('修改成功');history.back();</script>";
			} else {
				echo "<script>alert('修改失败');history.back();</script>";
			}
		} else {
			$s = M ( "System" );
			$data = $s->find ();
			$this->assign ( 'sdata', $data );
			$this->display ();
		}
	}
	public function top() {
		$this->display ();
	}
	public function main() {
		$this->display ();
	}
	public function footer() {
		$this->display ();
	}
}
?>